package models;

import java.util.LinkedList;
import java.util.Queue;

import tmts.ClientSendHandler;
import tmts.Server;

public class Skater extends Thread
{
    int id;
    int raceId;
    String name;
    String country;
    int totalDistance = 0;
    Queue<Lap> laps = new LinkedList<Lap>();

    @Override
    public void run()
    {
        try
        {
            Lap lap = laps.poll();

            sleepThread(lap);
            Thread.sleep(lap.getLaptime() / Server.simulateSpeed);

            while (Server.raceRunning && lap != null)
            {
                ClientSendHandler.pushNotification(this, lap);
                lap = laps.poll();
                sleepThread(lap);
            }
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    private void sleepThread(Lap lap)
    {
        if(lap == null) return;
        try
        {
            Thread.sleep(lap.getLaptime() / Server.simulateSpeed);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public int getSkaterId()
    {
        return id;
    }

    public void setSkaterId(int id)
    {
        this.id = id;
    }

    public String getSkaterName()
    {
        return name;
    }

    public void setSkaterName(String name)
    {
        this.name = name;
    }

    public String getCountry()
    {
        return country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }

    public void addLap(Lap lap)
    {
        laps.add(lap);
        totalDistance = Math.max(totalDistance, lap.getDistance());
    }
    
    public int getTotalDistance()
    {
        return totalDistance;
    }

    public Queue<Lap> getLaps()
    {
        return laps;
    }

    public void setRaceId(int raceId)
    {
        this.raceId = raceId;
    }
    
    public int getRaceId()
    {
        return this.raceId;
    }
}
